<template>
  <div>
    <!-- 头部 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input
          v-model="formInline.value"
          placeholder="用户名、备注……"
          size="mini"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="onSubmit"
          size="mini"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <div class="add">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleAddSubmit"
      >
        添加</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table ref="filterTable" :data="tableData" style="width: 100%">
      <el-table-column
        prop="id"
        label="Id"
        sortable
        width="180"
        column-key="date"
      >
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="100">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="100">
      </el-table-column>
      <el-table-column prop="isActived" label="是否启用" width="100">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.isActived ? 'primary' : 'danger'"
            disable-transitions
            >{{ scope.row.isActived ? "是" : "否" }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="isActived" label="是否删除" width="100">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.isDeleted ? 'danger' : 'primary'"
            disable-transitions
            >{{ scope.row.isDeleted ? "是" : "否" }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column prop="remarks" label="备注" width="100">
      </el-table-column>

      <el-table-column label="操作" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pager.pageIndex"
      :page-size="pager.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pager.count"
      background
    >
    </el-pagination>

    <!-- 弹出层 -->
    <Dialog
      :isdialogVisible="isdialogVisible"
      :data="{ row, index }"
      @updialogVisible="isdialogVisible = false"
      @upTableData="upTableData"
    />
  </div>
</template>

<script>
import Dialog from "./Dialog.vue";
import { GetUserList, deleteUser } from "../../api/user";
export default {
  data() {
    return {
      formInline: {
        value: "",
      },
      tableData: [],
      pager: {
        pageSize: 10,
        pageIndex: 1,
        count: 0,
      },
      isdialogVisible: false,
      row: null,
      index: 0,
    };
  },
  components: {
    Dialog,
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    getList(
      pager = {
        pageSize: this.pager.pageSize,
        pageIndex: this.pager.pageIndex,
      }
    ) {
      GetUserList(pager).then((res) => {
        this.tableData = res.data.data.data;
        this.pager = res.data.data.pager;
      });
    },
    // 列表记录条数改变触发事件
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.getList();
    },
    // 页码变化触发事件
    handleCurrentChange(val) {
      this.pager.pageIndex = val;
      this.getList();
    },
    //添加
    handleAddSubmit() {
      this.row = null;
      this.index = -1;
      this.isdialogVisible = true;
    },
    // 编辑
    handleEdit(index, row) {
      this.row = row;
      this.index = index;
      this.isdialogVisible = true;
    },
    // 删除
    handleDelete(index, row) {
      deleteUser(row.id)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData.splice(index, 1);
            this.pager.count--;
            this.$message({
              message: "删除成功",
              type: "success",
            });
          } else {
            this.$message({
              message: "删除失败",
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //搜索
    onSubmit() {
      let params = this.pager;
      params.q = this.formInline.value;
      this.getList(params);
    },
    upTableData(val) {
      if (this.index !== -1) {
        this.tableData.splice(this.index, 1, val);
      } else {
        this.tableData.push(val);
        this.pager.count++;
        this.pager.pageIndex =
          parseInt(this.pager.count / this.pager.pageSize) +
          (this.pager.count % this.pager.pageSize === 0 ? 0 : 1);
        this.getList();
      }
    },
  },
};
</script>